package stardust.core.network.protocol.http;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


public class Cookie
{

  private Map<String, String> entity = new HashMap<String, String>();

  public Cookie()
  {
  }

  public boolean isEmpty()
  {
    return this.entity.isEmpty();
  }

  public void clear()
  {
    this.entity.clear();
  }

  public void set(String cookieString)
  {
    String[] cs;
    String[] c;

    cs = cookieString.split(";");

    for (int i = 0; i < cs.length; i++)
    {
      c = cs[i].split("=", 2);

      if (c.length != 2)
      {
        continue;
      }

      this.entity.put(c[0].trim(), c[1].trim());
    }
  }

  public void set(String cookieName, String cookieValue)
  {
    this.entity.put(cookieName, cookieValue);
  }

  public String get(String cookieName)
  {
    return this.entity.get(cookieName);
  }

  public String get()
  {
    Iterator<String> i;
    String key;
    StringBuffer buffer;

    buffer = new StringBuffer();
    i = this.entity.keySet().iterator();

    while (i.hasNext())
    {
      key = i.next();

      buffer.append(key).append("=").append(this.get(key)).append(i.hasNext() ? ";" : "");
    }

    return buffer.toString();
  }

}
